<?
	   layout("/inc/win_layout.html",{title:sysTitle}){
?>
<div class="panel" style="height: 680px; overflow: auto;">
	<div class="wrapper" style="height: 600px; overflow: auto;">
 <div class="page-content">
    <header class="main-header">
        <nav class="navbar navbar-static-top">
            <div class="container">
                <div class="navbar-header">
                <button type="button" class="btn btn-blue btn-square">
                <b>日志</b>Console
                </button>
<!--                     <a href="#" class="navbar-brand"><b>日志</b>Console</a> -->
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse">
                        <i class="fa fa-bars"></i>
                    </button>
                </div>
                <div class="collapse navbar-collapse pull-left" id="navbar-collapse">
                    <ul class="nav navbar-nav">
                     &nbsp;	 &nbsp;
                    </ul>
                </div>
                <div class="navbar-custom-menu">
                    <ul class="nav navbar-nav">
                        <li>
                        <button type="button" class="btn btn-blue btn-square" onclick="window.location.reload()">
<!--                         <a href="javascript:window.location.reload();"> -->
                                <i class="fa fa-fw fa-refresh" ></i>刷新
<!--                             </a> -->
                            </button>
                            
                        </li>
                    </ul>
                </div>

            </div>
        </nav>
    </header>
        <pre class="logConsolePre"><div id="logConsole"></div><li class="fa fa-refresh fa-spin logConsoleRunning" ></li></pre>
    </div>
</div>
<br> <br> <br>
</div>
<?} ?>
<script>
var index1 = window.parent.getlay();

// 参数
var running = true;     // 允许运行
var executorAddress;
var triggerTime;
var logId;

$(function() {
	demo1();
});
function demo1() {
	$.getJSON('./xxljob_log_logDetailPage.do', {
		id : index1
	}, function(res) {
		if(res.logStatue.code == 200){
			running = true;
			$('.logConsoleRunning').show();
		    executorAddress = res.result[0].executorAddress;
		    triggerTime = res.result[0].triggerTime.time;
		    logId = res.result[0].id;
		}else{
			running = false;
			$('.logConsoleRunning').hide();
		    $('.logConsole').append(res.logStatue.msg);
		}
	});
};
$(function() {
	// valid
	if (!running) {
		return;
	}
	// 加载日志
	var fromLineNum = 0;
	var pullFailCount = 0;
	function pullLog() {
		// pullFailCount, max=20
		if (pullFailCount >= 20) {
			console.log("pullLog fail-count limit");
			running = false;
		}
		// valid
		if (!running) {
			$('.logConsoleRunning').hide();
			logRun = window.clearInterval(logRun)
			return;
		}
		// load
		console.log("pullLog, fromLineNum:" + fromLineNum);
		//alert(executorAddress+"  "+triggerTime+"  "+logId+" "+fromLineNum);
		$.ajax({
			type : 'POST',
			async: false,   // async, avoid js invoke pagelist before jobId data init
			url : "./xxljob_log_logDetailCat.do",
			data : {
				"executorAddress":executorAddress,
				"triggerTime":triggerTime,
				"logId":logId,
				"fromLineNum":fromLineNum
			},
			dataType : "json",
			success : function(data){
				pullFailCount++;
				if(data.result=="no"){
					console.log('pullLog fail');
					return;
				}
				if(fromLineNum != data.info.content[0].fromLineNum){
					console.log('pullLog fromLineNum not match');
					return;
				}
				if (fromLineNum == (data.info.content[0].toLineNum + 1) ) {
					console.log('pullLog already line-end');
					return;
				}
				// append
				fromLineNum = data.info.content[0].toLineNum + 1;
				$('#logConsole').append(data.info.content[0].logContent);
				pullFailCount = 0;
				// valid end
				if (data.info.content[0].end) {
					running = false;
					console.log("pullLog already file-end");
				}
			},error : function(data) {
				ComAlertTec.show(data.msg);
			}
		});
	}
	// 周期运行
	pullLog();
	var logRun = setInterval(function () {
		pullLog()
	}, 3000);
});
//提示-科技主题
var ComAlertTec = {
    html:function(){
        var html =
            '<div class="modal fade" id="ComAlertTec" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">' +
                '<div class="modal-dialog">' +
                    '<div class="modal-content-tec">' +
                        '<div class="modal-body"><div class="alert" style="color:#fff;"></div></div>' +
                        '<div class="modal-footer">' +
                            '<div class="text-center" >' +
                                '<button type="button" class="btn btn-blue ok" data-dismiss="modal" >确认</button>' +
                            '</div>' +
                        '</div>' +
                    '</div>' +
                '</div>' +
            '</div>';
        return html;
    },
    show:function(msg, callback){
        // dom init
        if ($('#ComAlertTec').length == 0){
            $('body').append(ComAlertTec.html());
        }

        // 弹框初始
        $('#ComAlertTec .alert').html(msg);
        $('#ComAlertTec').modal('show');

        $('#ComAlertTec .ok').click(function(){
            $('#ComAlertTec').modal('hide');
            if(typeof callback == 'function') {
                callback();
            }
        });
    }
};
</script>


